package com.lanhe.app.controller.packageManage;

import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.pckageManage.domain.LhPackageManage;
import com.lanhe.pckageManage.domain.vo.ManufacturerVo;
import com.lanhe.pckageManage.service.ILhPackageManageService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 包管理(LhPackageManage)表控制层
 *
 * @author zhanggl
 * @since 2023-12-02 22:51:11
 */
@RestController
@RequestMapping("/api/packageManage")
public class ApiPackageManageController extends BaseController {

	/**
	 * 服务对象
	 */
	@Resource
	private ILhPackageManageService lhPackageManageService;

	/**
	 * 分页查询所有数据
	 */
	@GetMapping("/list")
	public TableDataInfo list(LhPackageManage packageManage) {
		startPage();
		packageManage.setStatus(Constants.GeneralIntType.ZERO.toString());
		List<LhPackageManage> list = lhPackageManageService.selectIdentifierBindList(packageManage);
		for (LhPackageManage lhPackageManage : list) {
			lhPackageManage.setIsPopularName(lhPackageManage.getIsPopular().equals(Constants.GeneralIntType.ONE) ? "是" : "否");
		}
		return getDataTable(list);
	}

	/**
	 * 获取厂商
	 */
	@GetMapping("/getManufacturer")
	public AjaxResult getManufacturer() {
		List<LhPackageManage> list = lhPackageManageService.getManufacturer();

		// 拷贝对象
		List<ManufacturerVo> manufacturerVos = Optional.of(list).orElse(new ArrayList<>()).stream().map((item) -> {

			ManufacturerVo manufacturerVo = new ManufacturerVo();

			// 对象拷贝
			BeanUtils.copyProperties(item, manufacturerVo);
			return manufacturerVo;

		}).collect(Collectors.toList());
		return success(manufacturerVos);
	}

	/**
	 * 更新下载次数
	 */
	@GetMapping("/updateDownloadNumber")
	public AjaxResult updateDownloadNumber(@RequestParam(name = "packageId") Long packageId) {
		return success(lhPackageManageService.updateDownloadNumber(packageId));
	}
}

